//
//  EngineModel.m
//  quanminzhekou
//
//  Created by zxl on 15/1/20.
//  Copyright (c) 2015年 zhongqinglongtu. All rights reserved.
//

#import "EngineModel.h"

@implementation EngineModel

- (instancetype)init
{
    self = [super init];
    if (self) {
        _dic = [NSMutableDictionary new];
        _isNeedReloadData = NO;
        _isNeedUpdate = NO;
    }
    
    return self;
}

- (void)setZqStr:(NSString *)zqStr {
    _zqStr = zqStr;
    [_dic setObject:_zqStr forKey:@"zq"];
    _isNeedReloadData = YES;
}

- (void)setFlPoint:(CGPoint)flPoint {
    _flPoint = flPoint;
    
    //    if (!CGPointEqualToPoint(flPoint, CGPointZero)) {
    //        NSString* fl = [NSString stringWithFormat:@"%zd%@",
    //                        (NSInteger)(flPoint.x),
    //                        (0 == flPoint.y)? @"": [NSString stringWithFormat:@"-%zd", (NSInteger)(flPoint.y)]];
    //        [_dic setObject:fl forKey:@"fl"];
    //    } else {
    //        [_dic removeObjectForKey:@"fl"];
    //    }
    
    if (0 == _flPoint.x) {
        [_dic removeObjectForKey:@"fl1"];
        [_dic removeObjectForKey:@"fl2"];
    } else if (0 == _flPoint.y) {
        [_dic removeObjectForKey:@"fl2"];
        [_dic setObject:[NSString stringWithFormat:@"%zd", (NSInteger)_flPoint.x] forKey:@"fl1"];
    } else {
        [_dic removeObjectForKey:@"fl1"];
        [_dic setObject:[NSString stringWithFormat:@"%zd", (NSInteger)_flPoint.y] forKey:@"fl2"];
    }
    
    _isNeedReloadData = YES;
}

- (void)setPxStr:(NSString *)pxStr {
    if ([_pxStr isEqualToString:pxStr]) {
        return;
    }
    
    _pxStr = pxStr;
    if ([pxStr isKindOfClass:[NSString class]] && 0 != pxStr.length && ([pxStr isEqualToString:@"sellcount"] || [pxStr isEqualToString:@"now_price"])) {
        [_dic setObject:_pxStr forKey:@"px"];
    } else {
        [_dic removeObjectForKey:@"px"];
    }
    
    _isNeedReloadData = YES;
}

- (void)setFyStr:(NSString *)fyStr {
    if ([fyStr isEqualToString:@"0"] || [fyStr isEqualToString:@"1"]) {
        _isNeedReloadData = YES;
    }
    
    if ([_fyStr isEqualToString:fyStr]) {
        return;
    }
    
    _fyStr = fyStr;
    if ([fyStr isKindOfClass:[NSString class]] && 0 != fyStr.length && ![fyStr isEqualToString:@"0"] && ![fyStr isEqualToString:@"1"]) {
        [_dic setObject:_fyStr forKey:@"fy"];
    } else {
        [_dic removeObjectForKey:@"fy"];
    }
}

- (void)setAdStr:(NSString *)adStr {
    if ([_adStr isEqualToString:adStr]) {
        return;
    }
    
    _adStr = adStr;
    if ([adStr isKindOfClass:[NSString class]] && 0 != adStr.length && ([adStr isEqualToString:@"a"] || [adStr isEqualToString:@"d"])) {
        [_dic setObject:_adStr forKey:@"ad"];
    } else {
        [_dic removeObjectForKey:@"ad"];
    }
    
    _isNeedReloadData = YES;
}

- (void)setSxStr:(NSString *)sxStr {
    if ([_sxStr isEqualToString:sxStr]) {
        return;
    }
    
    _sxStr = sxStr;
    if ([sxStr isKindOfClass:[NSString class]] && 0 != sxStr.length) {
        [_dic setObject:_sxStr forKey:@"sx"];
    } else {
        [_dic removeObjectForKey:@"sx"];
    }
    
    _isNeedReloadData = YES;
}

- (void)setKwStr:(NSString *)kwStr {
    _kwStr = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)kwStr,NULL,CFSTR("!*'();:@&=+$,/?%#[]"),kCFStringEncodingUTF8));
    
    if ([kwStr isKindOfClass:[NSString class]] && 0 != kwStr.length) {
        [_dic setObject:_kwStr forKey:@"kw"];
    } else {
        [_dic removeObjectForKey:@"kw"];
    }
}

- (void)parameterBySeparatedUrlPath:(NSString*)urlPath {
    NSArray* arr = [urlPath componentsSeparatedByString:@"?"];
    NSString* subString = [arr objectAtIndex:1];
    NSArray* parameterArr = [subString componentsSeparatedByString:@"&"];
    for (NSString* str in parameterArr) {
        
        if (2 == [str rangeOfString:@"zq"].length) {
            self.zqStr = [[str componentsSeparatedByString:@"="] objectAtIndex:1];
        } else if (2 == [str rangeOfString:@"px"].length) {
            self.pxStr = [[str componentsSeparatedByString:@"="] objectAtIndex:1];
        } else if (2 == [str rangeOfString:@"fy"].length) {
            self.fyStr = [[str componentsSeparatedByString:@"="] objectAtIndex:1];
        } else if (2 == [str rangeOfString:@"ad"].length) {
            self.adStr = [[str componentsSeparatedByString:@"="] objectAtIndex:1];
        } else if (2 == [str rangeOfString:@"sx"].length) {
            self.sxStr = [[str componentsSeparatedByString:@"="] objectAtIndex:1];
        } else if (2 == [str rangeOfString:@"fl"].length) {
            NSString* flStr = [[str componentsSeparatedByString:@"="] objectAtIndex:1];
            NSArray* flArr = [flStr componentsSeparatedByString:@"-"];
            NSString* category = [flArr objectAtIndex:0];
            CGPoint point = CGPointZero;
            if (1 == flArr.count) {
                point = CGPointMake(category.integerValue, 0);
            } else {
                NSString* subCategory = [flArr objectAtIndex:1];
                point = CGPointMake(category.integerValue, subCategory.integerValue);
            }
            self.flPoint = point;
        } else if (2 == [str rangeOfString:@"kw"].length) {
            self.kwStr = [[str componentsSeparatedByString:@"="] objectAtIndex:1];
        }
    }
}

@end
